<?
class logger{

	public $isLogError=false;

	public function __construct(){
		// call the construct
		$this->init();
	}
	
	public function init(){
	}
	
	public function getFullUrl(){
		$ssl=$_SERVER["HTTPS"];
		$ssl=empty($ssl)?'':($ssl=="on")?"s":"";
		$protocol=$this->strleft(strtolower($_SERVER["SERVER_PROTOCOL"]),"/").$ssl;
		$port=($_SERVER["SERVER_PORT"]=="80")?"":(":".$_SERVER["SERVER_PORT"]);
		return($protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']);
	}

	public function strleft($s1,$s2){
		return substr($s1,0,strpos($s1,$s2));
	}

	public function record($data="",$logFile=NULL,$includeInfo=true){
		$result=false;
		$logFile=($logFile===NULL)?APP_LOG_FILE:$logFile;
		if(@filesize($logFile)>32000&&$logFile==APP_LOG_FILE){
			$this->newLog();
		}
		if($includeInfo){
			$data=date("Y/m/d H:i:s")."\t".$_SERVER['REMOTE_ADDR']."\t".$this->getFullUrl()."\t$data".chr(13);
		}
		#print("<p>Attempting to log data (file:$logFile,data:$data</p>");
		if(@file_put_contents($logFile,$data,FILE_APPEND)){
			$result=true;
		}
		return($result);
	}
	
	public function read($logFile=NULL){
		$result=false;
		$logFile=($logFile===NULL)?APP_LOG_FILE:$logFile;
		$result=@file_get_contents($logFile);
		return($result);
	}
	
	public function newLog($logFile=NULL){
		$result=false;
		$logFile=($logFile===NULL)?APP_LOG_FILE:$logFile;
		$fileName=split("\.",$logFile);
		$fileName=$fileName[0]."_".date("Ymd_His").".log";
		if(@rename($logFile,$fileName)){
			$result=$this->record("New Log File Created. Old log file called $fileName",$logFile);
		}
		return($result);
	}
};
?>